package cn.hysh.appium.keyworddriver;


import cn.hysh.appium.base.AndroidDriverBase;
import cn.hysh.appium.page.BasePage;
import cn.hysh.appium.util.GetByLocator;
import io.appium.java_client.android.AndroidElement;
import org.testng.Assert;

public class KeyWordsAction {
	 
    //声明静态 AndroidDriverBase 对象，用于在此类中相关 driver 的操作
	public static AndroidDriverBase driver;

	public static void input(String locator,String inputValue) {
		
		try {
			driver.findElement(GetByLocator.getLocator(locator))
					.sendKeys(inputValue);
		} catch (Exception e) {
			TestSuiteByExcel.testResult = false;
			e.printStackTrace();
		}
	}

	public static void click(String locator,String string) {
		try {
			driver.findElement(GetByLocator.getLocator(locator)).click();
		} catch (Exception e) {
			TestSuiteByExcel.testResult = false;
			e.printStackTrace();
		}
	}
	/**
	 * 通过坐标点击元素，对应关键字clickByCoordinate
	 * @param x
	 * @param y
	 */
	public static void clickByCoordinate(String locator,String coordinate){
		int x=0;
		int y=0;
		if(coordinate.contains("=")){
			x=Integer.valueOf(coordinate.split("=")[0]);
			y=Integer.valueOf(coordinate.split("=")[1]);
			try {
				driver.tap(1, x, y, 100);
			} catch (Exception e) {
				TestSuiteByExcel.testResult = false;
				e.printStackTrace();
			}
		}else{
			System.out.println("坐标无效");
			TestSuiteByExcel.testResult = false;
		}

	}

	public static void WaitFor_Element(String locator,String timeout) {
		try {
			//调用封装的 isElementExist 函数显示等待页面元素是否出现
			driver.isElementExist(GetByLocator.getLocator(locator),Integer.valueOf(timeout));
		} catch (Exception e) {
			TestSuiteByExcel.testResult = false;
			e.printStackTrace();
		}
	}

	public static void sleep(String string,String sleepTime){
		try {
			driver.wait(Integer.valueOf(sleepTime));
		} catch (Exception e) {
			TestSuiteByExcel.testResult = false;
			e.printStackTrace();
		}
	}
	
	public static void  Assert_String(String string,String assertString)  {
		try{ 
			System.out.println(driver.getPageSource());
			 Assert.assertTrue(driver.getPageSource().contains(assertString));
		} catch (AssertionError e) {
			TestSuiteByExcel.testResult = false;
			System.out.println("断言失败");
		}
	}
	public static void  Assert_Element(String string,String assertString)  {
		try{ 
			 Assert.assertEquals(driver.isElementExist(GetByLocator.getLocator(string)), true);
		} catch (AssertionError e) {
			TestSuiteByExcel.testResult = false;
			System.out.println("断言失败");
		}
	}
	/**
	 * 滑动屏幕
	 * @param string
	 * @param direction
	 */
	public static void swipeScreen(String swipeCounts,String direction){
		try {
			int count=Integer.valueOf(swipeCounts);
			while(count>0){
				driver.swipe(direction, 500);
				count--;
			}
			
		} catch (Exception e) {
			TestSuiteByExcel.testResult = false;
			e.printStackTrace();
		}
	}
	/**
	 * 在某元素上滑动
	 * @param locator
	 * @param direction
	 */
	public static void swipeOnElement(String locator,String direction){
		try {
			driver.swipeOnElement(GetByLocator.getLocator(locator), direction, 500);
		} catch (Exception e) {
			TestSuiteByExcel.testResult = false;
			e.printStackTrace();
		}
	}
	/**
	 * 长按某元素
	 * @param locator
	 * @param direction
	 */
	public static void longPress(String locator,String direction){
		try {
			driver.longPress(GetByLocator.getLocator(locator));
		} catch (Exception e) {
			TestSuiteByExcel.testResult = false;
			e.printStackTrace();
		}
	}
	/**
	 * 坐标点长按
	 * @param x
	 * @param y
	 */
	public static void longPressByCoordinate(String string,String coordinate){
		int x=0;
		int y=0;
		if(coordinate.contains("=")){
			x=Integer.valueOf(coordinate.split("=")[0]);
			y=Integer.valueOf(coordinate.split("=")[1]);
			try {
				driver.longPress(x,y);
			} catch (Exception e) {
				TestSuiteByExcel.testResult = false;
				e.printStackTrace();
			}
		}else{
			System.out.println("坐标无效");
		}
	}
	public static void wakeByGestures(String locator,String pwd){
		BasePage bp=new BasePage(driver);
		AndroidElement element=driver.findElement(GetByLocator.getLocator(locator));
		String[] indexsStr=pwd.split(",");
		int[] indexs=new int[indexsStr.length];
		for(int i=0;i<indexsStr.length;i++){
			indexs[i]=Integer.valueOf(indexsStr[i]);
		}
		bp.wakeByGestures(element, indexs);
	}
	/**
	 * 退出driver
	 */
	public static void quit(){
		driver.quit();
	}

}
